config-button.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useEffect, useRef, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import type { PopupProps } from './config-popup'
  6. import ConfigPopup from './config-popup'
  7. import cn from '@/utils/classnames'
  8. import Button from '@/app/components/base/button'
  9. import { Settings04 } from '@/app/components/base/icons/src/vender/line/general'
  10. import {
  11. PortalToFollowElem,
  12. PortalToFollowElemContent,
  13. PortalToFollowElemTrigger,
  14. } from '@/app/components/base/portal-to-follow-elem'
  15. const I18N_PREFIX = 'app.tracing'
  16. type Props = {
  17. readOnly: boolean
  18. className?: string
  19. hasConfigured: boolean
  20. controlShowPopup?: number
  21. } & PopupProps
  22. const ConfigBtn: FC<Props> = ({
  23. className,
  24. hasConfigured,
  25. controlShowPopup,
  26. ...popupProps
  27. }) => {
  28. const { t } = useTranslation()
  29. const [open, doSetOpen] = useState(false)
  30. const openRef = useRef(open)
  31. const setOpen = useCallback((v: boolean) => {
  32. doSetOpen(v)
  33. openRef.current = v
  34. }, [doSetOpen])
  35. const handleTrigger = useCallback(() => {
  36. setOpen(!openRef.current)
  37. }, [setOpen])
  38. useEffect(() => {
  39. if (controlShowPopup)
  40. // setOpen(!openRef.current)
  41. setOpen(true)
  42. // eslint-disable-next-line react-hooks/exhaustive-deps
  43. }, [controlShowPopup])
  44. if (popupProps.readOnly && !hasConfigured)
  45. return null
  46. const triggerContent = hasConfigured
  47. ? (
  48. <div className={cn(className, 'p-1 rounded-md hover:bg-black/5 cursor-pointer')}>
  49. <Settings04 className='w-4 h-4 text-gray-500' />
  50. </div>
  51. )
  52. : (
  53. <Button variant='primary'
  54. className={cn(className, '!h-8 !px-3 select-none')}
  55. >
  56. <Settings04 className='mr-1 w-4 h-4' />
  57. <span className='text-[13px]'>{t(`${I18N_PREFIX}.config`)}</span>
  58. </Button>
  59. )
  60. return (
  61. <PortalToFollowElem
  62. open={open}
  63. onOpenChange={setOpen}
  64. placement='bottom-end'
  65. offset={{
  66. mainAxis: 12,
  67. crossAxis: hasConfigured ? 8 : 0,
  68. }}
  69. >
  70. <PortalToFollowElemTrigger onClick={handleTrigger}>
  71. {triggerContent}
  72. </PortalToFollowElemTrigger>
  73. <PortalToFollowElemContent className='z-[11]'>
  74. <ConfigPopup {...popupProps} />
  75. </PortalToFollowElemContent>
  76. </PortalToFollowElem>
  77. )
  78. }
  79. export default React.memo(ConfigBtn)